package us.gaaoc.framework.webapp.util;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

public class DateConverter implements Converter {	
	 
	public Object getAsObject(FacesContext arg0, UIComponent component, String value) {
		if (value == null){
			return null;
		}		
		StringTokenizer tokenizer = new StringTokenizer(value, "/");		
		String monthStr = tokenizer.nextToken();
		String dayStr = tokenizer.nextToken();
		String yearStr = tokenizer.nextToken();
		
		int day = Integer.parseInt(dayStr);
		int month = Integer.parseInt(monthStr);
		int year = Integer.parseInt(yearStr);
		
		Calendar calendar = new GregorianCalendar();	
		calendar.set(Calendar.DATE, day);
		calendar.set(Calendar.MONTH, month-1);
		calendar.set(Calendar.YEAR, year);
		
		return calendar.getTime();
	}

	public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
		return value.toString();

	}

}
